package com.wjc.business;

import com.wjc.server.dto.CommonDto;
import com.wjc.server.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ControllersExceptionHandler {

    private static final Logger LOG = LoggerFactory.getLogger(ControllersExceptionHandler.class);

//    @ExceptionHandler(value = ValidatorException.class)
//    @ResponseBody
//    public ResponseDto validatorExceptionHandler(ValidatorException e) {
//        ResponseDto responseDto = new ResponseDto();
//        responseDto.setSuccess(false);
//        LOG.warn(e.getMessage());
//        responseDto.setMessage("请求参数异常！");
//        return responseDto;
//    }

    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public CommonDto businessExceptionHandler(BusinessException e) {
        CommonDto responseDto = new CommonDto();
        responseDto.setSuccess(false);
        LOG.error("业务异常：{}", e.getCode().getDesc());
        responseDto.setMessage(e.getCode().getDesc());
        return responseDto;
    }
}
